<template>
	<view style="padding: 40rpx 30rpx;">
		<view class="u-bg-fff u-r-20" style="padding: 50rpx 20rpx 0 20rpx;">
			<view class="u-flex-xy-center" style="flex-direction: column;" @click="uploadImage">
				<u-avatar size="150rpx"  :src='info.avatar'></u-avatar>
				<view class="u-m-t-20 u-f-40 u-bold">微信头像</view>
			</view>
			<u-gap height="40rpx"></u-gap>
			<view class="appInp">
				<view style="width: 110rpx;">昵称</view>
				<input placeholder="请输入" class="u-flex-1" style="text-align: right;" v-model="info.nickname"/>
			</view>
			<view class="appInp">
				<view style="width: 110rpx;">电话</view>
				<input placeholder="请输入手机号" class="u-flex-1"  v-model="info.username" type="number"/>
			</view>
			<view class="appInp">
				<view style="width: 110rpx;">验证码</view>
				<input placeholder="请输入验证码" class="u-flex-1" v-model="info.smscode" type="number"/>
				<view class="u-f-32" style="color: #209920;">
					<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
					<text @tap="getCode">{{tips}}</text>
				</view>
			</view>
		</view>
		<u-gap height="60rpx"></u-gap>
		<view class="appBtn" @click="updateData">确定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				info:{
					username:'',
					nickname:'',
					avatar:'',
					openid:'',
					smscode:'',
					
				},
			}
		},
		onLoad() {
			let wechat = uni.getStorageSync('wechat')
			this.info.nickname = wechat.nickName
			this.info.avatar = wechat.avatarUrl
			this.info.openid = wechat.openId
			this.info.username = wechat.username != undefined?wechat.username:''
		},
		methods: {
			async updateData(){

				let res = await uni.$u.http.post('login/register', this.info)
				uni.setStorageSync('userInfo', res)
				uni.setStorageSync('token', res.token)
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			async uploadImage() {
				var _this = this
				uni.chooseImage({
					count: 1, //默认9
					success(chooseImageRes) {
						uni.showLoading({
							title: '上传中'
						})
						const tempFilePaths = chooseImageRes.tempFilePaths;
						for (var i in tempFilePaths) {
							uni.$u.http.upload('/common/upload', {
								/* 会加在url上 */
								filePath: tempFilePaths[i],
								name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
								header: {
									token: uni.getStorageSync('token')
								}
							}).then(res => {
								_this.info.avatar = res.fullurl
								
							}).catch(err => {
								uni.hideLoading()
							})
						}
						_this.$nextTick(function() {
							uni.hideLoading()
						})
					}
				})
			},
			
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					// setTimeout(() => {
						
					let param = {
						mobile:this.info.username,
						type:'register'
					}
						
					await uni.$u.http.post('/sms/sendcode', param)
						
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					this.$refs.uCode.start();
						
					// }, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				// uni.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style>
	page{
		background-color: #f4f4f4;
	}
</style>
